import time

import network
import requests


def connected_wifi():
    wifi_name = "DoDayum-home"
    wifi_password = "970617baiyu"
    # 创建接口
    wlan = network.WLAN(network.STA_IF)
    # 激活接口
    wlan.active(True)
    # 扫描有那些wifi
    # wlan.scan()
    # 是否已经链接wifi
    is_connected = wlan.isconnected()
    if is_connected:
        # 已经链接上了,打印ip
        print("已经连接上了IP:\t", wlan.ifconfig()[0])
    else:
        # 没连上
        print("开始连接")
        wlan.connect(wifi_name, wifi_password)
        while True:
            if wlan.isconnected():
                print("连接成功,IP:\t", wlan.ifconfig()[0])
                break
            time.sleep(1)

def wifi_test():
    url = "https://www.baidu.com/"
    response = requests.get(url)
    if response.status_code == 200:
        print("请求成功")
        print(response.text)
    else:
        print("请求失败,状态码:\t", response.status_code)
